import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'

if (process.env.VUE_APP_USE_MOCK) {
  const mock = new MockAdapter(axios)

  mock.onPost('auth/login').reply(config => {
    return [
      200,
      { token: 'a token' },
    ]
  })

  mock.onGet('auth/info').reply(config => {
    log('mock onGet auth/info ', config)
    const token = config.headers.Authorization

    // 模拟token失效的情况
    if (token != 'a token') {
      return [401]
    }

    return [200, { name: '周霜霖' }]
  })

  mock.onPost('/auth/logout').reply(200)

  mock.onPost('/v1/notice')
    .reply(200, {
      code: 200,
      message: '',
      request_id: '42050',
      data: [
        {
          announcement_id: 1,
          type: 1,
          status: 1,
          content: '我就测试下公告我',
          created_at: 0,
        },
        {
          announcement_id: 2,
          type: 1,
          status: 1,
          content: '第二条',
          created_at: 0,
        },
        {
          announcement_id: 3,
          type: 1,
          status: 1,
          content: '第二条',
          created_at: 0,
        },
        {
          announcement_id: 4,
          type: 1,
          status: 1,
          content: '第二条',
          created_at: 0,
        },
      ],
    })

  mock.onPost('/v1/customer_service')
    .reply(200, {
      code: 200,
      message: '',
      request_id: '42050',
      data: {
        nickname: '哈哈哈',
        user_avatar: 'https://img.wiajhqa.com/uploads/20180828/42c281b958bf7f6f7ef7d157e67fd74a.jpg',
        user_qq: '5464645646',
      },
    })
}
